package com.wx.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.database.DatabaseDao;
import com.wx.po.TextMessage;
import com.wx.util.MessageUtil;
import com.wx.util.WeiXinUtil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
		
		String username = (String) request.getParameter("username");
		String phone = (String) request.getParameter("phone");
		String openid = (String) request.getSession().getAttribute("openid");
		String message = null;
		
		
//		System.out.println(username + " " + phone + " " + openid);
		try {
			if (DatabaseDao.addUser(username, phone, openid)) {
				WeiXinUtil.initUserTemplateMsg(username, phone, openid, "注册成功通知");
			} 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			WeiXinUtil.initUserTemplateMsg(username, phone, openid, "注册失败通知");
			e.printStackTrace();
		} finally {
			
		}
//		response.sendRedirect("userlist.jsp");
	}

}
